

import java.awt.event.ActionEvent;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.*;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Date;

import javax.swing.JFrame;
import javax.swing.JOptionPane;

public class Router extends Ingress_Frame {

	ServerSocket server;
	Socket client;
	String source_addr;
	int port_number;
	public String getSource_addr() {
		return source_addr;
	}

	public void setSource_addr(String source_addr) {
		this.source_addr = source_addr;
	}

	public int getPort_number() {
		return port_number;
	}

	public void setPort_number(int port_number) {
		this.port_number = port_number;
	}

	

	public Router() throws Throwable {
		
		getPortDetails();
		super.dis_ing_fra(port_number);
		storeInDb();
		startServer();
	}

	private void storeInDb() throws SQLException {
		boolean b = true;
		String qur1 = "select ipaddr,port from router";
		ResultSet rs = Queries.select(qur1);
		while (rs.next()) {
			if (rs.getString("ipaddr").equalsIgnoreCase(source_addr)
					&& rs.getInt("port") == port_number) {
				b = false;
			} else {
				b = true;

			}
		}
		if (b) {
			String qur = "insert into router(ipaddr,port) values (\""
					+ source_addr + "\"," + port_number + ")";
			String qur2 = "insert into message_delivery(ipaddr,port) values (\""
					+ source_addr + "\"," + port_number + ")";
			System.out.println(qur);
			Queries.update(qur);
			Queries.update(qur2);
			
		}

	}

	public void getPortDetails() {
		try {
			source_addr = InetAddress.getLocalHost().getHostAddress();
		} catch (UnknownHostException e) {
			e.printStackTrace();

		}
		port_number = Integer.parseInt(JOptionPane
				.showInputDialog("Enter the port number for this router :"));

	}

	public void startServer() throws Throwable {

		try {

			try {
				server = new ServerSocket(port_number);
			} catch (BindException b) {
				JOptionPane.showInternalMessageDialog(cp1,
						"router already started on this port: " + port_number);

			}

			JOptionPane.showInternalMessageDialog(cp1,
					"router is started listening on " + port_number);
			while (true) {
				client = server.accept();
				RouterThread rt = new RouterThread(client, this);
				Thread t = new Thread(rt);
				t.start();

			}
		} catch (IOException e) {
			e.printStackTrace();
		}

	}
	public synchronized  void updateTime(long duration) {

//		String query = "update router set duration=" + duration
//				+ " where port=" + r.getPort_number() + " and ipaddr=\""
//				+ r.getSource_addr() + "\"";
		
		String query="insert into utility values("+port_number+",\""+source_addr+"\","+duration+")"; 
		System.out.println(query);
		Queries.update(query);
	}
	
	@Override
	protected void sendButton(ActionEvent e) 
	{
		String message=Ing_data.getText();
		ResultSet rs=Queries.select("select * from node");
		try {
			while(rs.next())
			{
				if(rs.getString("name").equalsIgnoreCase("destination"))
				{
					int port=rs.getInt("port");
					String dest=rs.getString("ipaddr");
					try {
						Socket sock=new Socket(dest,port);
						RouterSendThread r=new RouterSendThread(sock,this,message);
						Thread td=new Thread(r);
						td.start();
//						DataOutputStream d=new DataOutputStream(sock.getOutputStream());
//						d.writeUTF(message);
//						DataInputStream di=new DataInputStream(sock.getInputStream());
//						String recv=di.readUTF();
//						JOptionPane.showInternalMessageDialog(cp1,recv);
					} catch (IOException e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();
					}
					
				}
					
			}
		} catch (SQLException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
			
		
		
	}

	public static void main(String[] args) throws Throwable {

		Router r = new Router();

	}

}
